package com.ccf.business.etl.flow;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.component.api.constants.ComponentConstants;
import com.component.api.model.dto.DefaultColumnDTO;
import com.component.api.model.dto.MapColumnDTO;
import com.common.utils.constant.Constants;
import com.common.utils.tool.StringUtil;
import com.common.model.exception.ParamFomatException;

import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author xianggj
 * @Date 2021/11/30 14:45
 **/
public class DefaultPaddingParamHandler extends ParamHandler {

    @Override
    protected Map<String, String> finishingParam() {
        Map<String, String> params = getParams();
        String columns = params.get(ComponentConstants.IN_COLUMNS);
        if (StringUtil.isJsonBlank(columns)){
            throw new ParamFomatException("缺少配置参数columns");
        }
        try {
            List<DefaultColumnDTO> defaultColumnDTOS = JSON.parseArray(columns, DefaultColumnDTO.class);
            for (DefaultColumnDTO defaultColumnDTO : defaultColumnDTOS) {
                String ruleVal = defaultColumnDTO.getRuleVal();
                switch (defaultColumnDTO.getRule()){
                    //已迁移统一输出
                    case DefaultColumnDTO.CALC:
                        ruleVal = StrUtil.replace(ruleVal, "%%", "F");
                        ruleVal = StrUtil.replace(ruleVal, "×", "*");
                        ruleVal = StrUtil.replace(ruleVal, "÷", "/");
                        ruleVal = StrUtil.replace(ruleVal, "x", "*");
                        ruleVal = StrUtil.replace(ruleVal, "X", "*");
                        defaultColumnDTO.setRuleFormula(ruleVal);
                        break;
                    case DefaultColumnDTO.RELY:
                        DefaultColumnDTO.Rely rely = JSON.parseObject(ruleVal, DefaultColumnDTO.Rely.class);
                        defaultColumnDTO.setRelyVal(rely);
                        break;
                        //自动不需要修改属性
                    default:
                        break;

                }
            }
            params.put(ComponentConstants.COLUMNS, JSON.toJSONString(defaultColumnDTOS));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ParamFomatException("参数格式错误");
        }
        return params;
    }
}
